package demo2;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class PlayCard {
    private static final String[] cardColor = {"♦","♣","♥","♠"};

    //购买52张牌
    public List<Card> buyCards() {
        List<Card> list = new ArrayList<>();
        for (int i = 1; i <= 13; i++) {
            for (int j = 0; j < 4; j++) {
                Card card = new Card(i,cardColor[j]);
                list.add(card);
            }
        }
        return list;
    }

    //洗牌
    public void shuffle(List<Card> list) {
        Random random = new Random();
        int size = list.size();
        for (int i = 0; i < size; i++) {
            int index = random.nextInt(size);//生成 0 ~ i-1 的随机数
            Card card = list.get(index);
            list.set(index,list.get(i));
            list.set(i,card);
        }
    }

    //发牌
    public List<List<Card>> deal(List<Card> cards) {
        List<List<Card>> list = new ArrayList<>();
        //创建三个人
        List<Card> list0 = new ArrayList<>();
        List<Card> list1 = new ArrayList<>();
        List<Card> list2 = new ArrayList<>();

        list.add(list0);
        list.add(list1);
        list.add(list2);

        int size = cards.size();
        int size2 = list.size();

        int count = 0;
        boolean flag = true;
        while(flag) {
            for (int j = 0; j < size2; j++) {
                list.get(j).add(cards.remove(0));
                count++;
                if(count == size){
                    flag = false;
                    break;
                }
            }
        }

        return list;
    }
}
